﻿using B.Auth.APi.Application.Command.Leaves;
using B.Auth.APi.Application.Command.Leaves.LeaveDto;
using B.Auth.Domain.Entity;
using B.Auth.Domain.Repository;
using B.Auth.Enum;
using MediatR;

namespace B.Auth.APi.Application.CommandHand.Leaves
{
    public class LeaveReprotCommandHand : IRequestHandler<LeaveReprotCommand, ApiResult<ApiPaging<List<LeaveReportDto>>>>
    {
        private readonly IBaseRepository<User> _user;
        private readonly IBaseRepository<LeaveModel> _leave;

        public LeaveReprotCommandHand(IBaseRepository<User> user, IBaseRepository<LeaveModel> leave)
        {
            _user = user;
            _leave = leave;
        }

        public Task<ApiResult<ApiPaging<List<LeaveReportDto>>>> Handle(LeaveReprotCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<ApiPaging<List<LeaveReportDto>>>();
            var user = _user.GetAll().Where(x => x.Del == 0).ToList();
            var leaves = _leave.GetAll().Where(x=>x.Del==0).ToList();
            List<LeaveReportDto> report = new List<LeaveReportDto>();
            foreach (var item in user)
            {
                report.Add(new()
                {
                    id = item.Id,
                    UserName =item.Name,
                    month1 = leaves.Where(x => x.LeaveName == item.Name && x.BeginTime.Month == 1).Sum(x => x.WorkTime),
                    month2 = leaves.Where(x => x.LeaveName == item.Name && x.BeginTime.Month == 2).Sum(x => x.WorkTime),
                    month3 = leaves.Where(x => x.LeaveName == item.Name && x.BeginTime.Month == 3).Sum(x => x.WorkTime),
                    month4 = leaves.Where(x => x.LeaveName == item.Name && x.BeginTime.Month == 4).Sum(x => x.WorkTime),
                    month5 = leaves.Where(x => x.LeaveName == item.Name && x.BeginTime.Month == 5).Sum(x => x.WorkTime),
                    month6 = leaves.Where(x => x.LeaveName == item.Name && x.BeginTime.Month == 6).Sum(x => x.WorkTime),
                    month7 = leaves.Where(x => x.LeaveName == item.Name && x.BeginTime.Month == 7).Sum(x => x.WorkTime),
                    month8 = leaves.Where(x => x.LeaveName == item.Name && x.BeginTime.Month == 8).Sum(x => x.WorkTime),
                    month9 = leaves.Where(x => x.LeaveName == item.Name && x.BeginTime.Month == 9).Sum(x => x.WorkTime),
                    month10 = leaves.Where(x => x.LeaveName == item.Name && x.BeginTime.Month == 10).Sum(x => x.WorkTime),
                    month11 = leaves.Where(x => x.LeaveName == item.Name && x.BeginTime.Month == 11).Sum(x => x.WorkTime),
                    month12 = leaves.Where(x => x.LeaveName == item.Name && x.BeginTime.Month == 12).Sum(x => x.WorkTime),
                });
            }

            var list = report.ToList();
            if (!string.IsNullOrEmpty(request.UserName))
            {

                list = list.Where(x => x.UserName.Contains(request.UserName)).ToList();

            }
            var count = list.Count();
            var pcount = (int)Math.Ceiling(count * 1.0 / request.size);
            var pagelist = list.OrderByDescending(x => x.id).Skip((request.index - 1) * request.size).Take(request.size);


            var data = new ApiPaging<List<LeaveReportDto>>();
            data.totalcount=count;
            data.totalpcount=pcount;
            data.data = pagelist.ToList();
            result.Message = "成功";
            result.data=data;
            return Task.FromResult(result);


        }
    }
}
